//
//  HMHomeToplistTVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-05.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMHomeToplistTVC.h"
#import "HMHomeToplistCell.h"
#import "HMHomeToplistModel.h"
#import "HMToplistDetailListVC.h"

@interface HMHomeToplistTVC ()

@property(strong,nonatomic)NSArray<HMHomeToplistModel *> *toplistModelArray;
@property(strong,nonatomic)HMHomeToplistModel *toplistModel;


@end
static NSString *TopListCellWithIdentifier = @"TopListCellWithIdentifier";
@implementation HMHomeToplistTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.estimatedRowHeight = 115.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
//    开启下拉刷新
    [self.tableView addTableViewRefreshHeaderWhenPullToPerformAction:@selector(loadData)];
    
//    加载页面数据
    [self loadData];
    //-------------设置导航栏----------------------
    self.navigationItem.title = @"榜单";
    
    [super setupNavRightItemsByTwo];
    
    
    //-------------设置导航栏结束----------------------
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.toplistModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HMHomeToplistCell *cell = [tableView dequeueReusableCellWithIdentifier:TopListCellWithIdentifier forIndexPath:indexPath];
    
    self.toplistModel = self.toplistModelArray[indexPath.row];
    //    给封面赋值
    [cell.coverPhotoImageView sd_setImageWithURL:[NSURL URLWithString:self.toplistModel.series_image_url] placeholderImage:[UIImage imageNamed:@"201612271441573272"]];
    
    cell.resourceNameLabel.text = self.toplistModel.series_name;
    cell.resourceContentLabel.text = self.toplistModel.series_content;
    
    return cell;
    
}

//选中某一行，跳转到榜单详情的列表页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HMToplistDetailListVC *toplistDetaillistVC = [[HMToplistDetailListVC alloc]init];
    
    toplistDetaillistVC.toplistModel = self.toplistModelArray[indexPath.row];
    toplistDetaillistVC.series_id = self.toplistModelArray[indexPath.row].series_id;
    
    
    [self.navigationController pushViewController:toplistDetaillistVC animated:YES];
}

- (void)loadData{
    [self.tableView registerClass:[HMHomeToplistCell class] forCellReuseIdentifier:TopListCellWithIdentifier];
    
    [HMHomeToplistModel loadHomeToplistDataFinished:^(id responseObject) {
        self.toplistModelArray = responseObject;
        
        [self.tableView reloadData];
        
//        关闭下拉刷新
        [self.tableView endRefresh];
    } Failed:^(NSError *error) {
        HMLog(@"error %@",error);
    }];

}
@end
